package sentinel.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import sentinel.handler.BlockHandler;
import sentinel.service.ProviderService;

import java.util.concurrent.TimeUnit;

@RestController
public class Provider8901Controller {

    @Value("${server.port}")
    private String port;


    /**
     * 测试流控模式-QPS
     */
    @GetMapping("/c1")
    public String c1(){
        return "provider-" + this.port + "-c1";
    }

    /**
     * 测试流控模式-线程数
     */
    @GetMapping("/c2")
    public String c2() throws InterruptedException{
        TimeUnit.SECONDS.sleep(3);
        return "provider-" + this.port + "-c2";
    }

    @Autowired
    private ProviderService providerService;
    /**
     * 测试流控模式-链路（暂无实现此功能）
     */
    @GetMapping("/c3")
    public String c3(){
        return "provider-" + this.port + "-c3-" + providerService.common();
    }
    /**
     * 测试流控模式-链路（暂无实现此功能）
     */
    @GetMapping("/c4")
    public String c4(){
        return "provider-" + this.port + "-c4-" + providerService.common();
    }

    /**
     * 自定义返回内容-function级
     */
    @GetMapping("/c5")      //当用/c5做资源名的时候对应这个
    @SentinelResource(value = "c5", blockHandler = "errorMessage")      //当用c5做资源名的时候对应这个
    public String c5(@RequestParam(value = "p1", required = false) String p1){
        return "provider-" + this.port + "-c5-" + p1;
    }

    public String errorMessage(String p1, BlockException exception){
        return "自定义错误提示";
    }

    /**
     * 自定义返回内容-class级
     */
    @GetMapping("/c6")
    @SentinelResource(value = "c6", blockHandlerClass = BlockHandler.class, blockHandler = "blockHandler")
    public String c6(){
        return "provider-" + this.port + "-c6";
    }

}
